﻿// FormAddin.cpp : Implementation of CFormAddin

#include "stdafx.h"
#include "FormAddin.h"
#include "SettingForm.h"
#include "ContactSync.h"
#include "Regesiter.h"
#include "Reminder.h"

#include <stdlib.h>
#include <sstream>

_ATL_FUNC_INFO OnClickSettingButtonInfo = {CC_STDCALL, VT_EMPTY, 2, {VT_DISPATCH,VT_BYREF | VT_BOOL }};
_ATL_FUNC_INFO OnClickSendButtonInfo = {CC_STDCALL, VT_EMPTY, 2, {VT_DISPATCH,VT_BYREF | VT_BOOL }};
_ATL_FUNC_INFO OnClickSyncSettingButtonInfo = {CC_STDCALL, VT_EMPTY, 2, {VT_DISPATCH,VT_BYREF | VT_BOOL }};
_ATL_FUNC_INFO OnClickSyncButtonInfo = {CC_STDCALL, VT_EMPTY, 2, {VT_DISPATCH,VT_BYREF | VT_BOOL }};
//_ATL_FUNC_INFO OnReminderAddInfo = {CC_STDCALL, VT_EMPTY, 1, {VT_BYREF | VT_VARIANT }};
//_ATL_FUNC_INFO OnReminderChangeInfo = {CC_STDCALL, VT_EMPTY, 1, {VT_BYREF | VT_VARIANT }};
//_ATL_FUNC_INFO OnReminderRemoveInfo = {CC_STDCALL , VT_EMPTY , 0 };

// CFormAddin
STDMETHODIMP CFormAddin::OnConnection(LPDISPATCH Application, ext_ConnectMode ConnectMode, LPDISPATCH AddInInst, SAFEARRAY * * custom)
{
	spAppCache = Application;//get the outlook application iteself
	ATLASSERT(spAppCache);
	//MessageBoxW(0,L"1",L"1",0);
	//---------------------------
	//Outlook 10.0.0.6515, 1033 (XP SP3) not totally support Unicode
	//Outlook 11.0.0.8169, 1033 (2003 SP3)
	//Outlook 12.0.0.4518, 1033 (2007)
	//Outlook 12.0.0.6423, 1033 (2007 SP2)
	//Outlook 14.0.0.4760, 1033 (2010)
	//---------------------------
	if(wcscmp(L"Outlook", spAppCache->Name.Detach()) != 0) return S_OK;
	auto spExplorer = spAppCache->ActiveExplorer();
	ATLASSERT(spExplorer);
	auto spCmdBars = spExplorer->CommandBars;
	ATLASSERT(spCmdBars);
	//for outlook 2003 and outlook xp
	if(spAppCache->Version.Detach()[1] <= '1'){
		CComVariant vName(L"OMS Client 2003 for SMS");
		CComVariant vPos(1);
		CComVariant vTemp(VARIANT_TRUE);
		CComVariant vEmpty(DISP_E_PARAMNOTFOUND , VT_ERROR);
		auto spNewBar = spCmdBars->Add(vName,vPos,vEmpty,vTemp);
		ATLASSERT(spNewBar);
		auto spNewBarCtrls = spNewBar->Controls;
		ATLASSERT(spNewBarCtrls);
		auto spNewCtrl1 = spNewBarCtrls->Add();
		ATLASSERT(spNewCtrl1);
		auto spNewCtrl2 = spNewBarCtrls->Add();
		ATLASSERT(spNewCtrl2);
		spButtonSetting = spNewCtrl1;
		spButtonSend = spNewCtrl2;
		spButtonSetting->Caption = L"SMS Setting";
		spButtonSetting->Enabled = true;
		spButtonSetting->Visible = true;
		spButtonSend->Caption = L"SMS Send";
		spButtonSend->Enabled = true;
		spButtonSend->Visible = true;
		spNewBar->Visible = true;
		IDispEventSimpleImpl<1,CFormAddin,&__uuidof(Office::_CommandBarButtonEvents)>::DispEventAdvise((IDispatch*)spButtonSetting);
		IDispEventSimpleImpl<2,CFormAddin,&__uuidof(Office::_CommandBarButtonEvents)>::DispEventAdvise((IDispatch*)spButtonSend);
	}

	//for outlook 2007
	if(spAppCache->Version.Detach()[1] <= '2'){
		CComVariant vName(L"OMS Client 2007 for Reminder");
		CComVariant vPos(1);
		CComVariant vTemp(VARIANT_TRUE);
		CComVariant vEmpty(DISP_E_PARAMNOTFOUND , VT_ERROR);
		auto spNewBar = spCmdBars->Add(vName,vPos,vEmpty,vTemp);
		ATLASSERT(spNewBar);
		auto spNewBarCtrls = spNewBar->Controls;
		ATLASSERT(spNewBarCtrls);
		auto spNewCtrl1 = spNewBarCtrls->Add();
		ATLASSERT(spNewCtrl1);
		auto spNewCtrl2 = spNewBarCtrls->Add();
		ATLASSERT(spNewCtrl2);
		spButtonSync = spNewCtrl1;
		spButtonSyncSetting = spNewCtrl2;
		spButtonSync->Caption = L"Contact Sync";
		spButtonSync->Enabled = false;
		spButtonSync->Visible = false;
		spButtonSyncSetting->Caption = L"Sync Setting";
		spButtonSyncSetting->Enabled = true;
		spButtonSyncSetting->Visible = true;
		spNewBar->Visible = true;
		IDispEventSimpleImpl<3,CFormAddin,&__uuidof(Office::_CommandBarButtonEvents)>::DispEventAdvise((IDispatch*)spButtonSync);
		IDispEventSimpleImpl<4,CFormAddin,&__uuidof(Office::_CommandBarButtonEvents)>::DispEventAdvise((IDispatch*)spButtonSyncSetting);
	}

	//for every version start tasks when connect
	ATLASSERT(spAppCache);
	if(!ctask)
		ctask = new ContactSyncTask(spAppCache);
	ctask->start();
	if(!rtask)
		rtask = new ReminderSyncTask(spAppCache);
	rtask->start();

	//IDispEventSimpleImpl<5,CFormAddin,&__uuidof(Outlook::ReminderCollectionEvents)>::DispEventAdvise(spAppCache);
	return S_OK;
}

STDMETHODIMP CFormAddin::OnDisconnection(ext_DisconnectMode RemoveMode, SAFEARRAY * * custom)
{
	ATLASSERT(spAppCache);
	//for outlook 2003 & outlook xp
	if(spAppCache->Version.Detach()[1] <= '1'){
		IDispEventSimpleImpl<1,CFormAddin,&__uuidof(Office::_CommandBarButtonEvents)>::DispEventUnadvise((IDispatch*)spButtonSetting);
		IDispEventSimpleImpl<2,CFormAddin,&__uuidof(Office::_CommandBarButtonEvents)>::DispEventUnadvise((IDispatch*)spButtonSend);
	}
	//for outlook 2007
	else if(spAppCache->Version.Detach()[1] <= '2'){
		IDispEventSimpleImpl<3,CFormAddin,&__uuidof(Office::_CommandBarButtonEvents)>::DispEventUnadvise((IDispatch*)spButtonSync);
		IDispEventSimpleImpl<4,CFormAddin,&__uuidof(Office::_CommandBarButtonEvents)>::DispEventUnadvise((IDispatch*)spButtonSyncSetting);
	}


	if(ctask){
		ctask->stop();
		ctask->wait();
	}
	if(rtask){
		rtask->stop();
		rtask->wait();
	}
	//IDispEventSimpleImpl<5,CFormAddin,&__uuidof(Outlook::ReminderCollectionEvents)>::DispEventUnadvise(spAppCache);
	return S_OK;
}

STDMETHODIMP CFormAddin::OnAddInsUpdate(SAFEARRAY * * custom)
{
	return S_OK;
}
STDMETHODIMP CFormAddin::OnStartupComplete(SAFEARRAY * * custom)
{
	return S_OK;
}
STDMETHODIMP CFormAddin::OnBeginShutdown(SAFEARRAY * * custom)
{
	return S_OK;
}

void __stdcall CFormAddin::OnClickSettingButton(IDispatch* Ctrl, VARIANT_BOOL* CancelDefault)
{
	ATLASSERT(spAppCache);
	
	if(!spSettingForm)
		spSettingForm = new CSettingForm();
	spSettingForm->DoModal();

}

void __stdcall CFormAddin::OnClickSendButton(IDispatch* Ctrl, VARIANT_BOOL* CancelDefault)
{
	ATLASSERT(spAppCache);
	// TODO : change to non modal
	if(!spSendForm)
		spSendForm = new CSendForm(spAppCache);
	spSendForm->DoModal();
	
	//if(!spSyncSettingForm->m_hWnd)
	//{
	//	spSyncSettingForm->Create(NULL, NULL);
	//	spSyncSettingForm->ShowWindow(SW_SHOW);
	//}

}


void __stdcall CFormAddin::OnClickSyncButton(IDispatch* Ctrl, VARIANT_BOOL* CancelDefault)
{
	//ATLASSERT(spAppCache);
	//if(!ctask)
	//	ctask = new ContactSyncTask(spAppCache);
	//ctask->start();
	//if(!rtask)
	//	rtask = new ReminderSyncTask(spAppCache);
	//rtask->start();

}
void __stdcall CFormAddin::OnClickSyncSettingButton(IDispatch* Ctrl, VARIANT_BOOL* CancelDefault)
{	
	if(!spSyncSettingForm)
		spSyncSettingForm = new CSyncSetting();
	spSyncSettingForm->DoModal();
	//if(!spSyncSettingForm)
	//	spSyncSettingForm = new CSyncSetting();
	//spSyncSettingForm->Create(NULL,NULL);
	//spSyncSettingForm->ShowWindow(SW_SHOW);

}





//void __stdcall CFormAddin::OnReminderAdd(Outlook::_Reminder* ReminderObject)
//{
//	MessageBoxW(0,ReminderObject->Caption,L"Add",0);
//}
//
//void __stdcall CFormAddin::OnReminderChange(Outlook::_Reminder* ReminderObject)
//{}
//
//void __stdcall CFormAddin::OnReminderRemove()
//{}
